﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TomatoTask  : Task
{
    private EventDelegate finishDelegate = null;
    private EventDelegate breakDelegate = null;
    private EventDelegate exitDelegate = null;
    private int shildBtnShowSecond = 0;

    public TomatoTask(TaskManager manager)
        : base(manager,TaskType.TomatoTask)
    {
        countDownSecond = PlayerPrefs.GetInt(TestKey.TomatoTask);
    }

    public override void Start()
    {
        base.Start();

        finishDelegate = new EventDelegate(OnClickFinish);
        breakDelegate = new EventDelegate(OnClickBreak);
        exitDelegate = new EventDelegate(OnClickExit);

        // 设置面板
        CountDownPanel.Instance.Reset();
        CountDownPanel.Instance.Open();
        CountDownPanel.Instance.SetCountDownTime(countDownSecond / 60, countDownSecond % 60);

        // 暂定第一次可以设置时间并且可以点击时钟以后启动，其余番茄钟的直接启动倒计时
        if (this.manager.GetTaskCount(this.GetTaskType()) == 1) 
        {
            CountDownPanel.Instance.SetClickCountDownClock(true, BeginCountDown);
            CountDownPanel.Instance.SetDragClock(true, true, SaveDefaultCoundDownTime);
        }
        else
        {
            CountDownPanel.Instance.SetFinishBtn(true, finishDelegate);
            CountDownPanel.Instance.SetBreakBtn(true, breakDelegate);
            CountDownPanel.Instance.SetEndBtn(true, exitDelegate);
            BeginCountDown();
        }
    }

    public override void BeginCountDown()
    {
        CountDownPanel.Instance.SetShildBtn(true, new EventDelegate(() =>
        {
            this.shildBtnShowSecond = 3;
            CountDownPanel.Instance.SetFinishBtn(true, finishDelegate);
            CountDownPanel.Instance.SetBreakBtn(true, breakDelegate);
            CountDownPanel.Instance.SetEndBtn(true, exitDelegate);
        }));

        base.BeginCountDown();
    }

    protected override void RunFunc(int passedSecond)
    {
        if (this.shildBtnShowSecond > 0)
        {
            --this.shildBtnShowSecond;
            if (this.shildBtnShowSecond <= 0)
            {
                CountDownPanel.Instance.SetFinishBtn(false, finishDelegate);
                CountDownPanel.Instance.SetBreakBtn(false, breakDelegate);
                CountDownPanel.Instance.SetEndBtn(false, exitDelegate);
            }
        }
        base.RunFunc(passedSecond);
    }
    void SaveDefaultCoundDownTime(bool isTure, int second)
    {
        if (!this.isRuning) return;

        countDownSecond = second;
        if (isTure)
        {
            PlayerPrefs.SetInt(TestKey.TomatoTask, second);
        }
    }

    public override void Stop()
    {
        finishDelegate.oneShot = true;
        breakDelegate.oneShot = true;
        exitDelegate.oneShot = true;
        finishDelegate.Clear();
        breakDelegate.Clear();
        exitDelegate.Clear();
        base.Stop();
    }

    void OnClickFinish()
    {
        this.Stop();
        InputCtrl.Instance.Open("请输入任务名称", (string inputStr) =>
        {
            this.manager.SetTaskName(inputStr);
            state = TaskStateType.WholeTaskFinish;
        });
    }

    void OnClickBreak()
    {
        this.Stop();
        this.customString = "  [中断]  ";
        state = TaskStateType.CurUtilFinish;
    }

    void OnClickExit()
    {
        if (!this.isRuning) return;
        NoticeCtrl.Instance.Open("你确定要结束整个任务吗？", (bool isYes) =>
        {
            if (isYes)
            {
                this.Stop();
                this.customString = "  [未完成]  ";
                state = TaskStateType.BackMainPanel;
            }
        });
    }


    //public override void Stop()
    //{
    //    base.Stop();
    //}
}
